/*
 * $Id$
 */
package py.firefly;

import py.firefly.request.Request;
import py.firefly.request.HttpRequest;
import py.firefly.request.Response;
import py.firefly.request.HttpResponse;
import py.firefly.request.RequestMapper;

import static py.firefly.resolution.Tiles.TILES_TEMPLATE;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import py.firefly.resolution.Resolution;
import py.firefly.util.Log;

//import org.apache.tiles.servlet.context.ServletTilesApplicationContext;
//import org.apache.tiles.startup.DefaultTilesInitializer;
/**
 *
 * @author myang
 */
public class FireflyFilter implements Filter {

    private RequestMapper _mapper;

    private static Log _log = Log.of("py.firefly");

    @Override
    public void init(final FilterConfig fc) throws ServletException {

//        initTiles(fc.getServletContext());

        final String tilesTemplate = fc.getInitParameter("tiles.template.path");
        if (null != tilesTemplate) {
            if (!tilesTemplate.startsWith("/")) {
                throw new FireflyException("template path must start with /");
            }
            fc.getServletContext().setAttribute(TILES_TEMPLATE, tilesTemplate);
        }

        final String bsc = fc.getInitParameter("bootstrap");
        if (null == bsc) {
            // what to do ?
        }
        try {
            final Class clz = Class.forName(bsc);
            final Bootstrap bs = (Bootstrap) clz.newInstance();
            _mapper = bs.boot();
        } catch (final Exception ex) {
            ex.printStackTrace();
            // what to do?
        }
    }

    /*
     private void initTiles(final ServletContext sc) {
     try {
     if (null != sc.getResource("/WEB-INF/tiles.xml")) {
     new DefaultTilesInitializer()
     .initialize(new ServletTilesApplicationContext(sc));
     }
     } catch (final MalformedURLException ex) {
     }
     }
     */
    @Override
    public void doFilter(final ServletRequest request,
                         final ServletResponse response,
                         final FilterChain chain)
      throws IOException, ServletException {
        final Request req = new HttpRequest((HttpServletRequest) request);
        _log.debug(req.uri());
        final Handler h = _mapper.map(req);
        if (h == null) {
            chain.doFilter(request, response);
        } 
        else {
            final Response resp = new HttpResponse((HttpServletResponse) response);
            final Resolution r = h.handle(req, resp);
            if (null != r) {
                r.execute(req, resp);
            }
        }
    }

    @Override
    public void destroy() {
        // no-op
    }
}
